package http;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @Author: ycy
 * @CreateDate: 2019/9/20 12:59
 */
public class UrlEncode {

  private static final String GSMARKLAND_RETURN_FIELDS = "id,word,type,eName,commentCount,commentScore,districtId,districtName"
          + ",pinyin,cityId,cityName,countryId,countryName,lat,lon,poiId,alias,score luceneScore,address,imageUrl";

  public static void main(String[] args) {
    StringBuilder req = new StringBuilder();
    req =  req.append("&section=1-");
    req.append(3);

    req.append("&sort=");
    req.append(urlEncode("distance(lon,lat,121.3609,31.226,true):asc,score:desc"));


    req.append("&query=");
    req.append(urlEncode("Type:gsmarkland AND " +  " \"distance(lon,lat,121.3609,31.226,true)<3\" " ));


    req.append("&return=");
    req.append(urlEncode(GSMARKLAND_RETURN_FIELDS));

    req.append("&debug=true");

    System.out.println(req.toString());

  }

  public static String urlEncode(String text) {
    try {
      if (StringUtils.isEmpty(text)) {
        return StringUtils.EMPTY;
      }
      return StringUtils.replace(URLEncoder.encode(text, StandardCharsets.UTF_8.name()), "+", "%20");
    } catch (UnsupportedEncodingException e) {}
    return text;
  }



}
